<?
global $db,$user,$topurl;
$release = "`release`";

$e = $db->query1("select * from node where id = ".$db->sqlint($params["page"]));

skin_param("page",$e);

$id = intval($params["id"]);

$e = $db->query1("select * from project where id = ".$db->sqlint($id)); 

$rid = intval($_REQUEST["release_id"]);
if (!$rid) {
	$re = $db->query1("select * from $release where project_id = $id order by datetimeon desc limit 1");
	$rid = $re["id"];
	}
if ($rid) {
$re = $db->query1("select * from $release where project_id = $id and id = ".$db->sqlint($rid));
}

echo "<h2>{$e["title"]}"; 
if ($re["title"]) echo " - {$re["title"]}"; 
echo "</h2>";

$uid = $e["users_id"];
if ($uid) {
	$ue = $db->query1("select * from users where id = ".$db->sqlint($uid));
	echo "<p>{$ue["title"]} ({$ue["name"]})</p>";
} else {
	echo "<p>This project has no maintainer.<br>";
	#if ($user["id"]) echo "<a href='dib.php'>Maintain this project.</a>";
	#else echo "<i>If you are the maintainer of this project, please sign up for an account and return to this page.</i>";
	echo "<i>If you are the maintainer of this project, please sign up for an account and contact <a href='mailto:philhassey@yahoo.com'>philhassey@yahoo.com</a> with your account name and project title.</i>";
	echo "</p>";
}
	
echo "<h3>Tags</h3>";
inc_inclib("tags.php");

if ($user["id"] && isset($_REQUEST["tags"])) {
    tags_update($id,$_REQUEST["tags"]);
}

$tags = tags_get($id);
$pre = "";
global $topurl;
if (count($tags)) {
    $qtags = array();
    foreach ($tags as $tag) { $qtags[] = $db->sqlstring($tag); }
    $r = $db->query("select *,count(*) as cnt from tags where value in (".implode(",",$qtags).") group by value order by cnt desc");
    tags_cloud($r);
}

if ($user["id"]) {
    global $user;
    echo "<form>";
    echo "<input type='text' name='tags' value=\"".implode(", ",$tags)."\" size=80> ";
    echo "<input type='submit' value='Update'>";
    echo "</form>";
}
	

if ($e["description"]) {
	echo "<h3>Description</h3>";
	echo "<div>{$e["description"]}</div>";
	}
	
if ($re["description"]) {
	echo "<h3>Changes</h3>";
	echo "<div>{$re["description"]}</div>";
	}
	
echo "<h3>Links</h3>";
$links = 0;

echo "<p>";	
echo "<table>";
if ($e["uri"]) {
	echo "<tr><td>Home Page:&nbsp;<td><a href=\"{$e["uri"]}\" target=\"_blank\">{$e["uri"]}</a>";
	$links++;
	}

foreach (array(
	"srcuri"=>"Source",
	"winuri"=>"Windows",
	"macuri"=>"Mac",
	) as $k=>$v) {
	if ($vv=$re[$k]) {
		echo "<tr><td>$v:&nbsp;<td><a href=\"$vv\">$vv</a>";
		$links++; 
		}
	}
echo "</table>";

if (!$links) {
	echo "The project maintainer not yet posted any links for this project.";
	}

echo "</p>";
	
global $topurl,$topdir;
inc_inclib("image.php");
$fname = "$topdir/shots/{$e["image"]}";

if ($e["image"] != "" && file_exists($fname)) {
echo "<h3>Screenshot</h3>";
$img = image_thumb($fname,400,300,"png");
list($iw,$ih) = getimagesize("$topdir/thumb/$img");
echo "<p><a href='$topurl/shots/{$e["image"]}' target='_blank'><img src='$topurl/thumb/$img' width=$iw height=$ih><br>click to view original size</a></p>";
}

$r = $db->query("select * from $release where project_id = ".$db->sqlint($id)." order by datetimeon desc");
if (count($r)) {
echo "<h3>Releases</h3>";
echo "<p>";
echo "<table>";
foreach ($r as $e) {
	echo "<tr>";
	if ($rid == $e["id"]) 
		echo "<td>{$e["title"]}&nbsp;";
		else 
		echo "<td><a href=\"?release_id={$e["id"]}\">{$e["title"]}</a>&nbsp;";
	echo "<td>- ".date("M j, Y",strtotime($e["datetimeon"]));
	}
echo "</table>";
echo "</p>";
}



echo "<h3>Comments</h3>";

if (!$user["id"]) {
echo "<p>If you wish to leave a comment, please <a href='$topurl/login/?direct=".urlencode($_SERVER["REQUEST_URI"])."'>sign in</a> first.</p>";
} else {


    if ($_POST["content"]) {
        $db->autoquery("insert","projectcomment",array(
            "project_id"=>$id,
            "datetimeon"=>date("Y-m-d H:i:s"),
            "users_id"=>$user["id"],
            "content"=>$_POST["content"],
            "rating"=>$_POST["rating"],
            ));
        header("Location: #"); die;
    } else {
        echo "<p>Please leave a comment:</p>";
        
    /*    if (!$user["id"]) {
            echo "<p>If you wish to be associated with your comment, please <a href='$topurl/login/?direct=".urlencode($_SERVER["REQUEST_URI"])."'>sign in</a> first.</p>";
            }*/
        
        echo "<form method=post>";
        echo "<p>";
        echo "<i>Worst</i> &nbsp; ";
        $pre = "";
        for ($i=1; $i<=5; $i++) {
            echo "$pre<input type='radio' name='rating' value='$i'>$i"; 
            $pre = " &nbsp; ";
            }
        echo " &nbsp; <i>Best</i>";
        echo "<br>";
        echo "<textarea name='content' rows=5 cols=80></textarea><br>";
        echo "<input type='submit' value='Add Comment'>";
        echo "</form>";
        echo "</p>";
        }
}

$action = (isset($_REQUEST["action"])?$_REQUEST["action"]:"none");
if ($action == "comments_delete") {
    if ($user["super"]) {
        $db->autoquery("delete","projectcomment",array("id"=>$_REQUEST["id"]));
    }
}

$r = $db->query("select projectcomment.*,users.title as users_title, users.name as users_name from projectcomment left join users on users.id = projectcomment.users_id where project_id = ".$db->sqlint($id)." order by datetimeon desc");
$n = count($r);

echo "<table class='comments'>";
if (!count($r)) { #NOTE: deprecated, but here in case i change my mind
    echo "<tr><th>No user comments have been submitted.";
    }
foreach ($r as $e) {
    if (!$e["users_title"]) $e["users_title"] = "Anonymous";
    echo "<tr><th align='left'>".date("F j, Y g:ia",strtotime($e["datetimeon"]));
    //var_dump($e);
    echo " - ".htmlentities($e["users_title"]) . " - nickname: (" . htmlentities($e["users_name"]) . ")";
    if($e["rating"]) {
        echo " - {$e["rating"]}/5";
        }
        
    echo "<tr><td><div>".str_replace("\n","<br>",htmlentities($e["content"]))."</div>";
    if ($user["super"]) {
        echo "<div><a href='?action=comments_delete&id={$e["id"]}'>delete</a></div>";
    }
    }
echo "</table>";


?>
